<?

class action_controller
{
	protected $layout = 'layout';
	protected $view;
	protected $renderer = 'html';
	protected $slots = array();

	protected $credentials = array();

	public $ssi_ttl = 0;

	public function execute() {}
	public function execute_post() {}
	public function execute_get() {}

	public function get_name()
	{
		return substr(get_class($this), 0, -7);
	}

	# Process control
	public function init()
	{
		$this->view = str_replace( '_', '/', $this->get_name() );
		$this->renderer = render::select( request::get_accept() );

		if ( strpos($_SERVER['REQUEST_URI'], '/ssi') === 0 )
		{
			$this->layout = null;
		}

		if ( $this->credentials )
		{
			if ( method_exists('user' , 'has_credentials') )
				$has = user::has_credentials( $this->credentials );
			else
				$has = session::has_credentials( $this->credentials );

			if ( !$has ) throw new access_exception( 'Access denied' );
		}
	}

	public function finalize()
	{
		if ( request::is_ajax() ) $this->layout = null;

		if ( request::is_dialog() && !$this->json['html'] ) $this->json = array(
			'html' => render_html::action($this),
			'title' => helper_html::$meta['title']
		);

		if ( isset ($this->json) ) $this->set_renderer('json');
	}

	# Rendering
	public function get_renderer()
	{
		return $this->renderer;
	}

	public function set_renderer( $renderer )
	{
		$this->renderer = $renderer;
	}

	public function get_view()
	{
		return $this->view;
	}

	public function get_layout()
	{
		return $this->layout;
	}

	public function __toString()
	{
		return $this->get_name();
	}
}